#include <fstream>
#include <iomanip>
using namespace std;

std::string getCurrentTime() {
    auto now = std::chrono::system_clock::now();
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                      now.time_since_epoch()) %
              1000;
    auto timer = std::chrono::system_clock::to_time_t(now);

    std::tm bt = *std::localtime(&timer);
    std::ostringstream oss;

    oss << std::put_time(&bt, "%Y-%m-%d %H:%M:%S");
    oss << '.' << std::setfill('0') << std::setw(3) << ms.count();

    return oss.str();
}

void test(std::string str) {
    std::ofstream myfile;
    myfile.open("d://temp//3dlog.txt", std::ios_base::app);
    // myfile.open("/Users/lmj/plugin.txt");
    //  Check if the file was opened successfully
    str = getCurrentTime() + "  " + str;
    if (myfile.is_open()) {
        // Write a line to the file
        myfile << str;
        // Close the file
        myfile.close();
    }
}


#define Log(str) test( str)
//#define LogDetail(str) test(str)
#define LogDetail(str) 


#include <chrono>
#include <string>
#include <unordered_map>
using namespace std;
#if 1
struct  TimeTrace {

    unordered_map<string, double> code_block_;
    unordered_map<string, _CHRONO time_point<_CHRONO system_clock>> code_block_point_;
    void begin_tick(string curr_block ) {
        
            tick1_ = std::chrono::system_clock::now();;

            code_block_point_[curr_block] = tick1_;
    }
    void end_tick(string curr_block) {

            auto tick2 = std::chrono::system_clock::now();
            std::chrono::duration<double> elapsed_seconds = tick2 - code_block_point_[curr_block];  // need to do  ,
            code_block_[curr_block] = code_block_[curr_block] + elapsed_seconds.count();
    }


    void write(std::string str) {

            std::ofstream myfile;
            myfile.open("d://temp//3D_time.txt", std::ios_base::app);
            // myfile.open("/Users/lmj/plugin.txt");
            //  Check if the file was opened successfully
            str = getCurrentTime() + "  " + str;
            if (myfile.is_open()) {
                // Write a line to the file
                myfile << str;
                // Close the file
                myfile.close();
            }
 
    
    }

    void dump() {

        for (auto  ele : code_block_) {

            auto time_in_ms =  std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::duration<double>(ele.second));
            string msg = "-----" + ele.first + "--" +
                         std::to_string(double(time_in_ms.count())) + " ms \n"; 
            write(msg);
        }
    
    }
    bool begin_ = false;
    bool end_ = true;
    _CHRONO time_point<_CHRONO system_clock> tick1_ ;
   // int tick2_ = 0;
};

#endif

TimeTrace g_time;


#define PRINT_TIME

#ifdef PRINT_TIME
#define BEGIN_MESURE(str) g_time.begin_tick(str)
#define END_MESURE(str) g_time.end_tick(str)
#define DUMP_MESURE g_time.dump()

#else
#define BEGIN_MESURE(str) 
#define END_MESURE(str) 
#define DUMP_MESURE 

#endif




















class FileAdapter {
 public:
  typedef void (*TXT_CB)(const std::string&);
  typedef void (*BIN_CB)(unordered_map<string, string>& msg);

  struct FileCloseDeleter {
    void operator()(ofstream* ofs) { ofs->close(); }
  };
  static FileAdapter& GetInstance() {
    static FileAdapter instance;
    return instance;
  }

  void flush_to_file(const string& files, const string& msg,
                     const string& path = "", int force_flush = 1) {
    lock_.lock();
    string file;
    file = path + files;

    if (fp_.find(file) != fp_.end()) {
      const auto& ofs = fp_.find(file)->second;
      *ofs << msg;

      if (force_flush) {
        ofs->flush();
      }
    } else {
      unique_ptr<std::ofstream, FileCloseDeleter> ofs(new std::ofstream);
      // ofs->open(file, std::ofstream::out | std::ofstream::app);
      ofs->open(file, std::ofstream::out);
      fp_.insert(make_pair(file, std::move(ofs)));

      *(fp_[file]) << msg;
      if (force_flush) {
        fp_[file]->flush();
      }
    }
    lock_.unlock();
  }
  void remove_tail() {}
  void read_txt(const string& file, TXT_CB cb) {
    ifstream fp("file");
    string str;
    while (getline(fp, str)) {
      cb(str);
    }
  }

 private:
  mutex lock_;
  unordered_map<string, unique_ptr<ofstream, FileCloseDeleter> > fp_;
  FileAdapter() {}
};

#define RESYNDEBUG
#ifdef RESYNDEBUG
#define LogA(msg) FileAdapter::GetInstance().flush_to_file("default.txt", msg)
#define LogAF(fp, msg) FileAdapter::GetInstance().flush_to_file(fp, msg)
#else
#define LogA(msg)
#endif

#include <fstream>
using namespace std;
void test(std::string str) {
  std::ofstream myfile;
  myfile.open("/Users/lmj/host.txt", std::ios_base::app);
  // myfile.open("/Users/lmj/plugin.txt");
  //  Check if the file was opened successfully
  if (myfile.is_open()) {
    // Write a line to the file
    myfile << str;
    // Close the file
    myfile.close();
  }
}

#define LogB(str) test(str)

int pre_in = -999;
int pre_value = 100;
int curr_value = 100;

#include <fstream>
void test(std::string str) {
  std::ofstream myfile;
  myfile.open("d://temp//bright.txt", std::ios_base::app);
  // myfile.open("/Users/lmj/plugin.txt");
  //  Check if the file was opened successfully
  if (myfile.is_open()) {
    // Write a line to the file
    myfile << str;
    // Close the file
    myfile.close();
  }
}

	 // 创建QElapsedTimer对象
	 QElapsedTimer timer;

	 // 开始计时
	 timer.start();
	 qint64 elapsedMicroseconds = timer.nsecsElapsed() / 1000; // 转换为微秒
	 cnt++;
	 sum = sum + elapsedMicroseconds;
	 int avg = sum / cnt;
	 test("curr " + std::to_string(elapsedMicroseconds) + " avg " +  std::to_string(avg) + "\n");
















#include <chrono>
#include <string>
#include <unordered_map>
#include <fstream>
using namespace std;
#if 1
struct  TimeTrace {

	unordered_map<string, double> code_block_;
	unordered_map<string, _CHRONO time_point<_CHRONO system_clock>> code_block_point_;
	void begin_tick(string curr_block) {

		tick1_ = std::chrono::system_clock::now();;

		code_block_point_[curr_block] = tick1_;
	}
	void end_tick(string curr_block) {

		auto tick2 = std::chrono::system_clock::now();
		std::chrono::duration<double> elapsed_seconds = tick2 - code_block_point_[curr_block];  // need to do  ,
		//int msecs = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count();// convert to ms ;
		int msecs = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed_seconds).count();// convert to ms ;
		//msecs = msecs / 1000000;
		code_block_[curr_block] = code_block_[curr_block] + msecs;
		cnt++;
	}

	void dump_time() {
		for (auto ele : code_block_) {
			int avg = code_block_[ele.first] /  cnt;
			string msg = ele.first +"---" + std::to_string(avg) + "\n";
			write(msg);

		}
	}

	void write(std::string str) {
		std::ofstream myfile;
		myfile.open("d://temp//bright.txt", std::ios_base::app);
		// myfile.open("/Users/lmj/plugin.txt");
		//  Check if the file was opened successfully
		if (myfile.is_open()) {
			// Write a line to the file
			myfile << str;
			// Close the file
			myfile.close();
		}
	}
	bool begin_ = false;
	bool end_ = true;
	_CHRONO time_point<_CHRONO system_clock> tick1_;
	int cnt = 0; 
	// int tick2_ = 0;
};

#endif


static TimeTrace g_time;




#define LogB(str) test(str)

int pre_in = -999;
int pre_value = 100;
int curr_value = 100;

if (pre_in == -999) {
  pre_in = dBright;
  pre_value = 2;
  curr_value = 2;
} else if (pre_in < dBright) {
  pre_in = dBright;
  // int temp = curr_value;
  curr_value = pre_value + 1;
  pre_value = curr_value;

} else if (pre_in > dBright) {
  pre_in = dBright;
  // int temp = curr_value;
  curr_value = pre_value - 1;
  pre_value = curr_value;
}

// uchar* qq = (uchar*) _data;
// for (int row = 0 ; rw)
// for (int i = 0; i < _step; i = i+ 3) {
// std:string msg = "(" + std::to_string(int(qq[i])) + "," +
// std::to_string(int(qq[i + 1])) + "," +  std::to_string(int(qq[i + 2])) +
// ")\n";
//     LogA(msg);
// }

std : string msg = to_string(x) + ",y:" + to_string(y) + "(" +
                   std::to_string(int(dump_pix.red())) + "," +
                   std::to_string(int(dump_pix.green())) + "," +
                   std::to_string(dump_pix.green()) + "),";
tests(msg);